<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Hash;
use DB;
use Mail;

class RegisterController extends Controller
{
    // 注册的首页
    public function getIndex()
    {
        return view('home.register.index');
    }  

    // 注册form表单处理
    public function postInsert(Request $request)
    {
        $s = $request -> only(['email','userpwd',]);
        $s['userpwd'] = Hash::make($s['userpwd']);
        $s['token'] = str_random(50);
        $s['createTime'] = time();
        $id = DB::table('shop_user') -> insertGetId($s);
        if($id){
            self::mailto($s['email'], $id, $s['token']);
            return view('home/register/success');
        }else{
             return view('home/register/fail');
        }

    }

    // 邮箱激活
    public function getActivation(Request $request)
    {
        $s = $request -> only(['id', 'token']);
        $arr['status'] = 2;
        $arr['token'] = str_random(50);
        $res = DB::table('shop_user') -> where('id', $s['id']) -> where('token', $s['token']) -> update($arr);
        if($res){
<<<<<<< HEAD
            return view('home/login/index');
=======
            return view('home/email/activation');
>>>>>>> 31e42f2d3415b82d4888522298db4e7de5f9c8aa
        }else{
            return view('home/email/fail');
        }
    }
    static public function mailto($email, $id, $token)
    {
        Mail::send('/home/email/index', ['id' => $id, 'token' => $token], function ($m) use ($email) {
            $m->to($email)->subject('衣秀网站注册邮件');
        });
    }

<<<<<<< HEAD
=======
    // 手机注册
    public function postPhone()
    {
        //短信接口地址
        $target = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
        //获取手机号
        $mobile = $_POST['mobile'];
        // var_dump($mobile);
        //生成的随机数
        // $mobile_code = rand(1000,9999);
        session(['phone_code' => rand(1000, 9999)]);
        $mobile_code = session('phone_code');
        if(empty($mobile)){
            exit('手机号码不能为空');
        }

        $post_data = "account=C71019810&password=d631f3cb7ed01726bed1e0ddcc66f699&mobile=".$mobile."&content=".rawurlencode("您的验证码是：".$mobile_code."。请不要把验证码泄露给其他人。");
        //用户名请登录用户中心->验证码、通知短信->帐户及签名设置->APIID
        //查看密码请登录用户中心->验证码、通知短信->帐户及签名设置->APIKEY
        $gets =  self::xml_to_array(self::Post($post_data, $target));
        if($gets['SubmitResult']['code']==2){
        }
        echo $gets['SubmitResult']['msg'];
    }

    static public function Post($curlPost,$url)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_NOBODY, true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
        $return_str = curl_exec($curl);
        curl_close($curl);
        return $return_str;
    }

     static public function xml_to_array($xml)
    {
        $reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
        if(preg_match_all($reg, $xml, $matches)){
            $count = count($matches[0]);
            for($i = 0; $i < $count; $i++){
            $subxml= $matches[2][$i];
            $key = $matches[1][$i];
                if(preg_match( $reg, $subxml )){
                    $arr[$key] = self::xml_to_array( $subxml );
                }else{
                    $arr[$key] = $subxml;
                }
            }
        }
        return $arr;
    }

    //手机注册
    public function postInsert2(Request $request)
    {
        // dd($request -> all())
        $s = $request -> only(['tel', 'userpwd']);
        $s['status'] = 2;
        $s['userpwd'] = Hash::make($s['userpwd']);
        $s['token'] = str_random(50);
        $s['createTime'] = time();
        $res = DB::table('shop_user') -> insert($s);
        if($res){
            return view('home/register/success');
        }else{
             return view('home/register/fail');
        }
    }
>>>>>>> 31e42f2d3415b82d4888522298db4e7de5f9c8aa

}
